/**
 * Created by TomSean on 2018/1/8.
 */
var CouponReport = function ($parent) {
    this.$parent = $parent;
    this.sysUtils = new kendoUtils();
    this.init();
    return this;
}
CouponReport.prototype = {
    init : function () {
        var $this = this;
        var fieldArray = this.sysUtils.getBindFieldArray(this.$parent.find('.search-panel'));
        var functionArray = [
            {fName : 'find_data', fs : this.findReport},
            {fName : 'reset', fs : this.resetInfo}
        ];
        this.models = new bindBaseModel(null, fieldArray, functionArray, null);
        this.sysUtils.bindDoucment(this.models, this.$parent, this);
        this.$parent.find("#coupon-use-reports-frame").height(this.$parent.height() - this.$parent.find(".search-panel").outerHeight());
        this.findReport();
    },
    findReport : function (e) {
        var $this = this;
        if (!isEmpty(e)) {
            $this = $(e.target).data('that');
        }
        var min = $this.$parent.find('#minTime').val();
        var max = $this.$parent.find('#maxTime').val();
        $this.$parent.find('#searchYear').html(min + ' ~ ' + max);
        var dataT = $this.sysUtils.getBindDocJson($this.models.bindFieldArray);
        JsonpAjax(baseApiPath + 'preferential/coupon/report', JSON.stringify(dataT), 'json', function (res) {
            var list = res;
            $this.createReport(list);
        }, false);
    },

    createReport : function (list) {
        var $this = this;
        var $table = $this.$parent.find('#tableTemp').find("table").clone();
        var total = 0;
        var headerTr = '<tr id="header-tr">';
        headerTr += '<th>商户名称</th>';
        headerTr += '<th>优惠项目</th>';
        headerTr += '<th>未使用的优惠券总计(元)</th>';
        headerTr += '<th>已使用的优惠券总计(元)</th>';
        headerTr += '<th>管理处分配优惠(元) / 商家分配优惠(元)</th>';
        headerTr += '</tr>';
        $table.append(headerTr);
        if (!isEmpty(list)) {
            var tempMerchantId = '';
            var isRowspan = false;
            var x = 1;
            var smallTotalUse = 0;
            var smallTotalNoUse = 0;
            var smallTotalSystem = 0;
            var smallTotalSelf = 0;
            for (var i = 0; i < list.length; i++) {
                var tr = '<tr>';
                var merchantId = list[i]['MERCHANT_ID'];
                var merchantName = list[i]['MERCHANT_NAME'];
                if (tempMerchantId != merchantId) {
                    if (!isEmpty(tempMerchantId)) {
                        $table.append('<tr style="background: #eee;"><td style="text-align: right">小计：</td><td>'+smallTotalNoUse+'</td><td>'+smallTotalUse+'</td></tr>');
                    }
                    $table.find('td[data-me="'+tempMerchantId+'"]').attr('rowspan', (x+1));
                    tempMerchantId = merchantId;
                    x = 0;
                    smallTotalUse = 0;
                    smallTotalNoUse = 0;
                    var smallTotalSystem = 0;
                    var smallTotalSelf = 0;
                    isRowspan = true;
                    tr += '<td data-me="'+merchantId+'"><p style="font-size:14px;">'+merchantName+'</p></td>';
                }
                var preferentialCode = list[i]['PREFERENTIAL_CODE'];
                var preferentialName = list[i]['PREFERENTIAL_NAME'];
                var noUseMoney = list[i]['NO_USE_MONEY'];
                var useMoney = list[i]['USE_MONEY'];
                var systemCreate = list[i]['SYSTEM_CREATE'];
                var selfCreate = list[i]['SELF_CREATE'];
                tr += '<td data-pr="'+preferentialCode+'"><p style="font-size:14px;">'+preferentialName+' <small style="font-size:12px;">('+preferentialCode+')</small></p></td>';
                tr += '<td><span style="font-weight: bold;">'+noUseMoney+'</span></td>';
                smallTotalNoUse += noUseMoney;
                tr += '<td><span style="font-weight: bold;">'+useMoney+'</span></td>';
                smallTotalUse += useMoney;
                tr += '<td>'+systemCreate+' / '+selfCreate+'</td>';
                smallTotalSystem += systemCreate;
                smallTotalSelf += selfCreate;
                total += useMoney;
                tr += '</tr>';
                $table.append(tr);
                x++;
                isRowspan = false;
            }
            if (!isRowspan) {
                $table.append('<tr style="background: #eee;"><td style="text-align: right">小计：</td><td>'+smallTotalNoUse+'</td><td>'+smallTotalUse+'</td><td>'+smallTotalSystem+' / '+smallTotalSelf+'</td></tr>');
                $table.find('td[data-me="' + tempMerchantId + '"]').attr('rowspan', (x+1));
            }
            $this.$parent.find('#allTotal').html(total);
        } else {
            $table.append('<tr><td colspan="5" style="text-align: center;"><p style="color:#ccc;padding:10px;font-size:18px;font-weight: 100;">暂无数据</p></td></tr>');
        }
        $table.show();
        $this.$parent.find('.tableDiv').html($table);
    },
    /*
     * 重置按钮
     */
    resetInfo:function(e){
        var that = $(e.target).data("that");
        that.sysUtils.setBindFieldNull(that.models.bindFieldArray);
        that.sysUtils.mvvmModel.set("beginTime", new Date().pattern("yyyy"));
    },
    /**
     * 打印
     * @param e
     */
    print:function(e){
        this.$parent.find("#month-rent-div").find("div:first").printArea({
            mode:"popup",
            popTitle:"",
            popClose:true,
            popHt:600,
            popWd:1000
        });
    }
}